package com.taoyi.mhouse.binder;
/**
 * created by tempo on 2015/11/14.
 */

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.taoy.recyclebinder.DataBindAdapter;
import com.taoy.recyclebinder.DataBinder;
import com.taoyi.mhouse.R;
import com.taoyi.mhouse.bean.BroadcastingAnimal;
import com.taoyi.mhouse.util.UIUtils;
import com.tempo.widget.SectionBar;

import java.util.ArrayList;
import java.util.List;

public class BroadcastingBinder extends DataBinder<BroadcastingBinder.ViewHolder> {

    public BroadcastingBinder(DataBindAdapter dataBindAdapter) {
        super(dataBindAdapter);
    }

    @Override
    public ViewHolder newViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(UIUtils.getContext()).inflate( R.layout.binder_broadcasting,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void bindViewHolder(ViewHolder holder, int position) {
        holder.mSectionBar.setOnSectionBarClickListtener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(UIUtils.getContext(),"broasting see more clicked",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerView mRecycleView;
        public SectionBar mSectionBar;
        public ViewHolder(View itemView) {
            super(itemView);
            mSectionBar=(SectionBar)itemView.findViewById(R.id.sectionbar);
            mRecycleView = (RecyclerView) itemView.findViewById(R.id.rv_broadcasting);
            mRecycleView.setHasFixedSize(true);
            LinearLayoutManager layoutManager = new LinearLayoutManager(UIUtils.getContext());
            layoutManager.setOrientation(LinearLayout.HORIZONTAL);
            mRecycleView.setLayoutManager(layoutManager);
            MyAdapter myAdapter=new MyAdapter();
            //todo
            List<BroadcastingAnimal> data=new ArrayList<BroadcastingAnimal>();
            for(int i=0;i<7;i++){
                BroadcastingAnimal anim=new BroadcastingAnimal();
                anim.hint=i+"更新到第三话";
                anim.title=i+"刀剑神域";
                data.add(anim);
            }
            myAdapter.setData(data);
            mRecycleView.setAdapter(myAdapter);
        }
    }

  static  class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
        private List<BroadcastingAnimal> data;
        private List<Integer> imgs;
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v =LayoutInflater.from(UIUtils.getContext()).inflate( R.layout.item_broadcasting, parent,false);
            return new MyViewHolder(v);
        }

        public void setData(List<BroadcastingAnimal> data) {
            this.data = data;
            imgs = new ArrayList<>();
            imgs.add(R.drawable.cast1);
            imgs.add(R.drawable.cast2);
            imgs.add(R.drawable.cast3);
            imgs.add(R.drawable.cast4);
            imgs.add(R.drawable.cast7);
            imgs.add(R.drawable.cast6);
            imgs.add(R.drawable.cast8);
        }

        @Override
        public int getItemCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            BroadcastingAnimal anime = data.get(position);
            holder.iv.setImageResource(imgs.get(position));
            holder.tvName.setText(anime.title);
            holder.tvHint.setText(anime.hint);
        }


    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv;
        public TextView tvName;
        public TextView tvHint;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);
            tvName = (TextView) itemView.findViewById(R.id.tv_broadcasting_name);
            tvHint = (TextView) itemView.findViewById(R.id.tv_broadcasting_hint);
        }

    }
}
